package com.cskaoyan.factory;

import com.cskaoyan.bean.Car;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @author stone
 * @date 2021/11/18 15:51
 */
public class CarProxyFactoryBean implements FactoryBean<Car> {
    @Override
    public Car getObject() throws Exception {
        //cglib的依赖不需要单独引入
        Car car = Car.class.newInstance();
        Car carProxy = (Car) Enhancer.create(Car.class, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] objects) throws Throwable {
                System.out.println("刷卡 滴滴");
                Object invoke = method.invoke(car, objects);
                return invoke;
            }
        });

        return carProxy;
    }

    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }
}
